<?php
/**
 * Created by PhpStorm.
 * User: zhangxilu
 * Date: 2017/12/24
 * Time: 下午9:50
 */

namespace common\logic;


use common\models\HouseTypeImageModel;
use yii\db\ActiveQuery;

class HouseTypeImageLogic extends Logic
{

    /**
     * @return ActiveQuery
     */
    protected function _getActiveQuery()
    {
        return HouseTypeImageModel::find();
    }

    public function save(HouseTypeImageModel $model){
        return $model->save();
    }

    /**
     * @param $houseId
     *
     * @return array|HouseTypeImageModel[]
     */
    public function getAll($houseId){
        return $this->_all(['house_id'=>$houseId]);
    }

    public function All(){
        return $this->_all();
    }

    //保存文件
    public function saveFile($house_id,$key){
        if ($_FILES && !empty($_FILES['HouseTypeImageModel']['name']['img_path'][$key])) {
            $filePath = "/upload/house_image_type/{$house_id}/";
            $fileName = $filePath . uniqid() . '.' . pathinfo($_FILES['HouseTypeImageModel']['name']['img_path'][$key], PATHINFO_EXTENSION);
            $filePath = \Yii::getAlias('@webroot') . $filePath;
            if (!file_exists($filePath)) {
                mkdir($filePath, 0777, true);
            }
            $savePath = \Yii::getAlias('@webroot') . $fileName;
            if (move_uploaded_file($_FILES['HouseTypeImageModel']['tmp_name']['img_path'][$key], $savePath)) {
                return $fileName;
            }
        }
        return '';
    }
}